package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.basic.WmsStorehouse;
import com.canaan.business.service.basic.IWmsStorehouseService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.canaan.business.mapper.basic.StorehousePermissionMapper;
import com.canaan.business.domain.basic.StorehousePermission;
import com.canaan.business.service.basic.IStorehousePermissionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仓库权限Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Service
public class StorehousePermissionServiceImpl implements IStorehousePermissionService {
    @Resource
    private StorehousePermissionMapper storehousePermissionMapper;
    @Resource
    private IWmsStorehouseService wmsStorehouseService;

    /**
     * 查询仓库权限
     *
     * @param id 仓库权限主键
     * @return 仓库权限
     */
    @Override
    public StorehousePermission selectStorehousePermissionById(Long id) {
        return storehousePermissionMapper.selectStorehousePermissionById(id);
    }

    /**
     * 查询仓库权限列表
     *
     * @param storehousePermission 仓库权限
     * @return 仓库权限
     */
    @Override
    public List<StorehousePermission> selectStorehousePermissionList(StorehousePermission storehousePermission) {
        return storehousePermissionMapper.selectStorehousePermissionList(storehousePermission);
    }

    /**
     * 新增仓库权限
     *
     * @param storehousePermission 仓库权限
     * @return 结果
     */
    @Override
    public int insertStorehousePermission(StorehousePermission storehousePermission) {
        this.checkNameCount(storehousePermission.getName(),storehousePermission.getId());
        if (StringUtils.isBlank(storehousePermission.getNo())) {
            storehousePermission.setNo(TableNoUtil.getNo(StorehousePermission.class));
        }
        storehousePermission.setCreateBy(SecurityUtils.getUsername());
        storehousePermission.setCreateTime(DateUtils.getNowDate());
        return storehousePermissionMapper.insertStorehousePermission(storehousePermission);
    }

    /**
     * 判断权限名称是否重复
     *
     * @param name 名称
     * @param id id
     */
    private void checkNameCount(String name, Long id) {
        int nameCount = this.storehousePermissionMapper.checkNameCount(name,id);
        if (nameCount > 0) {
            throw new ServiceException("存在相同名称的仓库权限，请修改");
        }
    }

    /**
     * 批量新增仓库权限
     *
     * @param storehousePermissionList 仓库权限列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehousePermission(List<StorehousePermission> storehousePermissionList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehousePermission storehousePermission : storehousePermissionList) {
            if (StringUtils.isBlank(storehousePermission.getNo())) {
                storehousePermission.setNo(TableNoUtil.getNo(StorehousePermission.class));
            }
            storehousePermission.setCreateBy(username);
            storehousePermission.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i = 0; i < storehousePermissionList.size(); ) {
            int endIndex = i + 500;
            if (endIndex > storehousePermissionList.size()) {
                endIndex = storehousePermissionList.size();
            }
            insertNum = insertNum + storehousePermissionMapper.batchInsertStorehousePermission(storehousePermissionList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改仓库权限
     *
     * @param storehousePermission 仓库权限
     * @return 结果
     */
    @Override
    public int updateStorehousePermission(StorehousePermission storehousePermission) {
        this.checkNameCount(storehousePermission.getName(),storehousePermission.getId());
        storehousePermission.setUpdateBy(SecurityUtils.getUsername());
        storehousePermission.setUpdateTime(DateUtils.getNowDate());
        return storehousePermissionMapper.updateStorehousePermission(storehousePermission);
    }

    /**
     * 批量修改仓库权限
     *
     * @param storehousePermissionList 仓库权限列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehousePermission(List<StorehousePermission> storehousePermissionList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehousePermission storehousePermission : storehousePermissionList) {
            storehousePermission.setUpdateBy(username);
            storehousePermission.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i = 0; i < storehousePermissionList.size(); ) {
            int endIndex = i + 500;
            if (endIndex > storehousePermissionList.size()) {
                endIndex = storehousePermissionList.size();
            }
            updateNum = updateNum + storehousePermissionMapper.batchUpdateStorehousePermission(storehousePermissionList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除仓库权限
     *
     * @param nos 需要删除的仓库权限主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehousePermissionByIds(String[] nos) {
        // 需要先校验是否被引用
        int countUsedStorehouse = this.storehousePermissionMapper.getCountUsedStorehouseByNos(nos);
        if (countUsedStorehouse > 0) {
            throw new ServiceException("选中的数据已被仓库关联，无法删除");
        }
        return storehousePermissionMapper.deleteStorehousePermissionByIds(nos);
    }

    /**
     * 删除仓库权限信息
     *
     * @param id 仓库权限主键
     * @return 结果
     */
    @Override
    public int deleteStorehousePermissionById(Long id) {
        return storehousePermissionMapper.deleteStorehousePermissionById(id);
    }

    /**
     * 查询仓库权限
     *
     * @param no 编号
     * @return 仓库权限
     */
    @Override
    public StorehousePermission selectStorehousePermissionByNo(String no) {
        return storehousePermissionMapper.selectStorehousePermissionByNo(no);
    }

    /**
     * 批量删除仓库权限
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStorehousePermissionByNos(String[] nos) {
        return storehousePermissionMapper.deleteStorehousePermissionByNos(nos);
    }

    /**
     * 删除仓库权限信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStorehousePermissionByNo(String no) {
        return storehousePermissionMapper.deleteStorehousePermissionByNo(no);
    }

    /**
     * 根据仓库编号，查询仓库权限列表
     *
     * @param storehouseNo 仓库编号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StorehousePermission getPermissionsByStorehouseNo(String storehouseNo){
        WmsStorehouse storehouse = wmsStorehouseService.selectWmsStorehouseByNo(storehouseNo);
        if (storehouse == null){
            throw new ServiceException("仓库编号有误");
        }
        if (StringUtils.isEmpty(storehouse.getPermissionNo())){
            throw new ServiceException("该仓库未配置仓库权限");
        }
        return this.selectStorehousePermissionByNo(storehouse.getPermissionNo());
    }
}
